package firing;

import game.Bullet;
import game.Enemy;
import game.Player;

import java.util.List;

/** Node that will follow one branch, then the other in a cycle */
public class FlipFlopBranch extends AbstractBinaryNode {
    
    private static final long serialVersionUID = 1532292873914028967L;
    /** determines whether to choose left or right node */
    private int frameThreshold;
    private int cycleLength;
    private int currentFrame;
   
    public FlipFlopBranch() {
	super();
	randomizeNode();
    }
    
    public void calculate(List<Bullet> bullets, double angle, Enemy shooter, Player player) {
	currentFrame++;
	if (currentFrame >= cycleLength) {
	    currentFrame = 0;
	}
	if (currentFrame < frameThreshold && left != null) {
	    left.calculate(bullets, angle, shooter, player);
	} else if (right != null){
	    right.calculate(bullets, angle, shooter, player);
	}
    }

    @Override
    public void randomizeNode() {
	frameThreshold = (int) (Math.random() * 10);
	cycleLength = frameThreshold + (int) (Math.random() * 10);
    }
    
    public void setFrameThreshold(int threshold) {
	frameThreshold = threshold;
    }
    
    public int getFrameThreshold() {
	return frameThreshold;
    }
    
    public void setCycleLength(int length) {
	cycleLength = length;
    }
    
    public int getCycleLength() {
	return cycleLength;
    }
    
    public String toString() {
	return ("FlipFlop: " + frameThreshold + "/" + cycleLength);
    }

    @Override
    public Node duplicate() {
	FlipFlopBranch duplicate = new FlipFlopBranch();
	duplicate.cycleLength = cycleLength;
	duplicate.frameThreshold = frameThreshold;
	if (left != null) {
	    duplicate.left = left.duplicate();
	}
	if (right != null) {
	    duplicate.right = right.duplicate();
	}
	return duplicate;
    }
}
